#include "GEnginePCH.h"
#include "Effects.h"
#include "Support.h"

namespace Effects
{

D3DXHANDLE   WVP;
D3DXHANDLE   WorldInvTrans;
D3DXHANDLE   FinalXForms;	
D3DXHANDLE   EyePos;
D3DXHANDLE   World;
D3DXHANDLE   Tex;
D3DXHANDLE   Mtrl0;
D3DXHANDLE   Light;
D3DXHANDLE   Tech;

ID3DXEffect* FX[10];
int nrFX=1;

D3DXHANDLE GetWVP() { return WVP; }
D3DXHANDLE GetLight() { return Light; }
D3DXHANDLE GetEyePos() { return EyePos; }
D3DXHANDLE GetFinalXForms() { return FinalXForms; }
D3DXHANDLE GetWorldInvTrans() { return WorldInvTrans; }
D3DXHANDLE GetWorld() { return World; }
D3DXHANDLE GetTex() { return Tex; }
D3DXHANDLE GetMtrl0() { return Mtrl0; }
D3DXHANDLE GetTech() { return Tech; }

ID3DXEffect* getEffect(int index)
{
	return FX[index];
}
void Init()
{
	// Create the FX from a .fx file.
	ID3DXBuffer* errors = 0;
	D3DXCreateEffectFromFile(d3ddev, "../data/vblend2.fx", 
		0, 0, D3DXSHADER_DEBUG, 0, &FX[0], &errors);
	if( errors )
		MessageBox(0, (char*)errors->GetBufferPointer(), 0, 0);

	// Obtain handles.
	Tech            = FX[0]->GetTechniqueByName("VBlend2Tech");
	WVP             = FX[0]->GetParameterByName(0, "gWVP");
	WorldInvTrans   = FX[0]->GetParameterByName(0, "gWorldInvTrans");
	FinalXForms     = FX[0]->GetParameterByName(0, "gFinalXForms");
	Mtrl0           = FX[0]->GetParameterByName(0, "gMtrl");
	Light           = FX[0]->GetParameterByName(0, "gLight");
	EyePos          = FX[0]->GetParameterByName(0, "gEyePosW");
	World           = FX[0]->GetParameterByName(0, "gWorld");
	Tex             = FX[0]->GetParameterByName(0, "gTex");
}
void invalidateDeviceObjects(void)
{
	for(int i = 0; i < nrFX; i++)
	{
		if( FX[i] != NULL )
		{
			int nNewRefCount = FX[i]->Release();

			if( nNewRefCount > 0 )
			{
				static char strError[255];
				sprintf_s( strError, "The Effect %d object failed to cleanup properly.\n"
					"Release() returned a reference count of %d", i, nNewRefCount );
				MessageBox( NULL, strError, "ERROR", MB_OK | MB_ICONEXCLAMATION );
			}

			FX[i] = NULL;
		}
	}
}

}